package com.lysj.admin.cus.wxcustomer.controller;

import com.lysj.admin.cus.wxcustomer.constant.VerifyCodeApplicationRunner;
import com.lysj.admin.cus.wxcustomer.domain.WxCustomer;
import com.lysj.admin.cus.wxcustomer.feign.AuthFeign;
import com.lysj.admin.cus.wxcustomer.service.WxCustomerService;
import com.lysj.admin.cus.wxcustomer.util.WxAuzLogin;
import com.lysj.admin.utils.exception.BaseException;
import com.lysj.admin.utils.spring.foundation.base.BaseController;
import com.lysj.admin.utils.web.ParamUtil;
import com.lysj.admin.utils.web.RandomUtil;
import com.lysj.admin.utils.web.Resp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Map;

/**
 * @author Created by drj on 2019-03-04 19:55:36
 * @description 微信用户控制层
 */
@Slf4j
@RestController
@RequestMapping("/customer/management")
public class WxCustomerController extends BaseController<WxCustomer> {


    @Resource
    private WxCustomerService wxCustomerService;

    @Override
    public WxCustomerService getService() {
        return wxCustomerService;
    }

    @Resource
    private AuthFeign authFeign;


    /*
     * @author drj
     * @date 2019-03-26 10:46
     * @Description :发送手机验证码
     */
    @GetMapping("/send_phone_verify_code")
    public void sendPhoneVerifyCode(String phone) {
        if (ParamUtil.isBlank(phone)) {
            throw new BaseException("请输入手机号码", Resp.Status.PARAM_ERROR.getCode());
        }
        String code = RandomUtil.getRandomNum(6);
        boolean flag = authFeign.sendSms(code, phone);
        if (flag) {
            throw new BaseException("短信验证码发送失败,请重试", Resp.Status.PARAM_ERROR.getCode());
        }
        //将手机号码跟验证码存入缓存中
        VerifyCodeApplicationRunner.verifyCodeCache.put(phone, code);
    }

    /*
     * @author drj
     * @date 2019-03-26 14:49
     * @Description :绑定手机号
     */
    @PostMapping("/bingding")
    public Resp bingding(String customerId, String phone, String verifyCode) {
        if (ParamUtil.isBlank(verifyCode)) {
            return new Resp().error(Resp.Status.PARAM_ERROR, "验证码不能为空");
        }
        //清理过期缓存
        VerifyCodeApplicationRunner.verifyCodeCache.prune();
        //从缓存中获取该手机的校验码
        String phoneCode = VerifyCodeApplicationRunner.verifyCodeCache.get(phone, false);
        if (ParamUtil.isBlank(phoneCode)) {
            return new Resp().error(Resp.Status.PARAM_ERROR, "验证码已过期,请重试");
        }
        if (!phoneCode.equals(verifyCode)) {
            return new Resp().error(Resp.Status.PARAM_ERROR, "验证码输入有误");
        }
        WxCustomer wxCustomer = wxCustomerService.findOne(customerId);
        wxCustomer.setPhone(phone);
        wxCustomerService.save(wxCustomer);
        return new Resp().success("手机号码绑定成功");
    }

    /*
     * @author drj
     * @date 2019-03-26 14:49
     * @Description :授权登录
     */
    @GetMapping(value = "/login")
    public Resp wxLogin() {

        response.setCharacterEncoding("UTF-8");
        String code = request.getParameter("code");
        System.out.println("用户授权码:" + code);
        Map<String, String> map = WxAuzLogin.getOpenId(code);
        String openid = map.get("openid");
        if (ParamUtil.isBlank(openid)) {
            throw new BaseException("授权失败");
        }
        WxCustomer userQuery = wxCustomerService.getRepository().findByOpenId(openid); //通过数据库查询返回的User
        if (null == userQuery) { // 数据库还没有数据,新建一个用户
            userQuery = new WxCustomer();
            userQuery.setOpenId(openid);
            userQuery = wxCustomerService.getRepository().saveAndFlush(userQuery);
        }
        return new Resp().success(userQuery.getId(), "");
    }

}
